package org.firebug.spring.boot.mybatis.repository.dao;

import org.apache.ibatis.annotations.*;
import org.firebug.spring.boot.mybatis.repository.po.UserPO;
import org.firebug.spring.boot.mybatis.repository.qo.SalaryQO;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface UserDAO {
    @Options(useGeneratedKeys = true)
    @Insert("insert into t_user (name,salary) values (#{name},#{salary})")
    int insert(UserPO po);

    @Insert("<script>" +
            "insert into t_user (name,salary) values " +
            "<foreach collection='pos' item='item' index='index' separator=','>" +
            "(#{item.name},#{item.salary})" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("pos") List<UserPO> pos);

    @Delete("delete from t_user where id=${id}")
    int deleteById(@Param("id") int id);

    @Delete("<script>" +
            "delete from t_user where id in " +
            "<foreach collection='ids' item='item' index='index' open='(' separator=',' close=')'>" +
            "${item}" +
            "</foreach>" +
            "</script>")
    int deleteByIds(@Param("ids") int... ids);

    @Update("update t_user set is_deleted=1 where id=${id}")
    int delete2ById(@Param("id") int id);

    @Update("<script>" +
            "update t_user set is_deleted=1 where id in " +
            "<foreach collection='ids' item='item' index='index' open='(' separator=',' close=')'>" +
            "${item}" +
            "</foreach>" +
            "</script>")
    int delete2ByIds(@Param("ids") int... ids);

    @Update("<script>" +
            "update t_user " +
            "<trim prefix='set' suffixOverrides=','>" +
            "<if test='name!=null and name!=\"\"'>name=#{name},</if>" +
            "<if test='salary!=null and salary!=\"\"'>,salary=#{salary},</if>" +
            "</trim>" +
            " where id=${id}" +
            "</script>")
//    @Update("update t_user set name=#{name},salary=#{salary} where id=${id}")
    int updateById(UserPO po);

    @Select("select * from t_user where id=${id}")
    UserPO selectById(@Param("id") int id);

    @Select("<script>" +
            "select * from t_user where id in " +
            "<foreach collection='ids' item='item' index='index' open='(' separator=',' close=')'>" +
            "${item}" +
            "</foreach>" +
            "</script>")
    List<UserPO> selectByIds(@Param("ids") int... ids);

    @Select("select count(id) from t_user where is_deleted=0")
    int selectCount();

    @Select("select coalesce(sum(salary),0) from t_user where is_deleted=0")
    BigDecimal selectSumSalary();

    @Select("select * from t_user where id>=(select id from t_user where is_deleted=0 limit ${offset},1) and is_deleted=0 limit ${limit}")
    List<UserPO> select(@Param("offset") int offset, @Param("limit") int limit);

//    @Select("<script>" +
//            "select * from t_user where is_deleted=0" +
//            "<if test='minSalary!=null and minSalary!=\"\"'> and salary=#{minSalary}</if>" +
//            "<if test='maxSalary!=null and maxSalary!=\"\"'> and salary=#{maxSalary}</if>" +
//            "<if test='name!=null and name!=\"\"'> and name like concat(#{name},'%')</if>" +
//            "</script>")
    @Select("select * from t_user where is_deleted=0 and salary>=#{minSalary} and salary<=#{maxSalary} and name like concat(#{name},'%')")
    List<UserPO> selectByQO(SalaryQO qo);
}